package com.xidian.zhiancloud.controller.equipFacility.baseSetting;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.OhType;
import com.xidian.zhiancloud.qo.equipFacility.OhTypeQO;
import com.xidian.zhiancloud.service.equipFacility.OhTypeService;
import com.xidian.zhiancloud.vo.equipFacility.OhTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * 设备设施 基础设置 检修类型设置接口
 *
 * @Author: 任启亮
 * @Date: 2020/11/12
 */
@RestController
@RequestMapping("/OhType")
public class OhTypeController extends BaseController {
    @Autowired
    private OhTypeService ohTypeService;

    /**
     * 增加检修类型设置
     *
     * @param ohType 检修类型设置对象
     * @return 是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(OhType ohType) {
        ohType.setCreatorId(getSessionUserid());
        ohType.setCompanyId(getSessionUserCompanyId());
        ohType.setAddTime(new Date(System.currentTimeMillis()));
        Boolean result = ohTypeService.save(ohType);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取检修类型设置
     *
     * @param id 检修类型设置id
     * @return 检修类型设置对象
     */
    @GetMapping("/getById")
    public ResultDTO<OhType> getById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(ohTypeService.getById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除检修类型设置
     *
     * @param ids ids数组
     * @return 返回删除标志
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean result = ohTypeService.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改检修类型设置
     *
     * @param ohType 检修类型设置对象
     * @return 返回修改状态
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(OhType ohType) {
        ohType.setCreatorId(null);
        ohType.setCompanyId(null);
        ohType.setAddTime(null);
        Boolean result = ohTypeService.updateById(ohType);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * @param ohTypeQO 传递参数
     * @return 检修类型设置分页查询
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<OhTypeVO>> getList(OhTypeQO ohTypeQO) {
        if (ohTypeQO.getCurrent()== null || ohTypeQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        ohTypeQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(ohTypeService.selectByQO(ohTypeQO), GeneralResponseEnums.SUCCESS);
    }
}
